package com.cy.law.home.web;

import com.cy.law.home.service.HomeService;
import com.cy.law.utils.SessionUtil;
import com.cy.law.vo.RequestResult;
import com.cy.law.vo.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class HomeController {
    @Autowired
    HomeService homeService;

    @RequestMapping(value = "home/nav/getHumanNavItem", method = RequestMethod.POST)
    public RequestResult getHumanNavItemHandler(HttpServletRequest request) {
        return homeService.getHumanNavItem(request);
    }


    @RequestMapping(value = "law/human/getHumanInfoById", method = RequestMethod.GET)
    public RequestResult getLawHumanInfoByIDHandler(HttpServletRequest request) {
        int humanId = ServletRequestUtils.getIntParameter(request, "humanId", -1);
        if (humanId == -1) {
            return RequestResult.new1();
        } else {
            return homeService.getHumanInfoById(humanId);
        }
    }

    @RequestMapping(value = "law/human/getLevelTree", method = RequestMethod.POST)
    public List<Tree> getLawPageHumanTreeHandler(HttpServletRequest request) {
        Integer unitId = SessionUtil.getUnitId(request);
        if (unitId == null) {
            return null;
        } else {
            return homeService.getHumanLevelTree(unitId);
        }
    }


    @RequestMapping(value = "home/rec/getReportList", method = RequestMethod.POST)
    public RequestResult getReportListHandler(HttpServletRequest request) {
        String deviceId = ServletRequestUtils.getStringParameter(request, "deviceId", "");
        int currentPage = ServletRequestUtils.getIntParameter(request, "currentPage", 1);
        int numPerPage = ServletRequestUtils.getIntParameter(request, "numPerPage", 10);
        return homeService.getReportList(deviceId, currentPage, numPerPage);
    }
}